<?php

namespace App\CustHub\Service;

use App\CustHub\Mapper\ActiveAddFriendContentMapper;
use Hyperf\DbConnection\Db;
use Mine\Abstracts\AbstractService;
use Mine\MineModel;

class ActiveAddFriendContentService extends AbstractService
{
    public $mapper;

    public function __construct(ActiveAddFriendContentMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * 获取群内容列表
     * @param array $params
     * @return array
     */
    public function contentList(array $params)
    {
        $query = $this->mapper->model::query()
            ->from('ch_add_friend_content as a')
            ->select(['a.group_id','a.id', 'a.content','a.pass_time','a.add_time','a.status','b.group_name',Db::raw('CASE WHEN a.add_time > 0 THEN a.pass_time / a.add_time ELSE 0 END AS pass_rate')])
            ->leftJoin('ch_add_friend_group as b', 'b.id', '=', 'a.group_id')
            ->where('a.uid', $params['uid'])
            //->whereIn('a.group_id', $groupArray)
            ->paginate((int)$params['page_size']??MineModel::PAGE_SIZE);
        $query->getCollection()->transform(function ($item) {
            $item['group_id']==0 && $item['group_name'] = '默认分组';
            return $item;
        });

        return $this->mapper->setPaginate($query);
    }

}